package com.yhj.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author yaohj
 * @date 2024年11月20日 15:35
 */
@Configuration
@MapperScan(basePackages = { "com.yhj.mapper"}, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisMasterConfig {

    @Autowired
    @Qualifier("master")
    private DataSource master;

    @Bean
    public MybatisSqlSessionFactoryBean masterSqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(master);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mappers/*.xml"));
        return sessionFactory;
    }

   /* @Bean
    public SqlSessionTemplate masterSqlSessionTemplate() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(masterSqlSessionFactory()); // 使用上面配置的Factory
        return template;
    }*/
}
